import Taro, { Component } from '@tarojs/taro'
import { View, Image,Button } from '@tarojs/components'
import { connect } from '@tarojs/redux'

import { user, usered } from './../../actions/counter'
import allExtrance from './../../config/allExtrance'
import phoneLogin from './../../api/phoneLogin'
import Public from './../../api/public'
import './getUserInfo.less'

@connect(({ counter }) => ({
    counter
}), (dispatch) => ({
    user() {
        dispatch(user())
    },
    usered() {
        dispatch(usered())
    },
}))
class GetUserInfo extends Component {
    constructor(props) {
        super(props)
        this.state = {
        }
    }


    componentWillReceiveProps(nextProps) {
        console.log(nextProps.counter, "nextprops")
    }

    componentDidMount() {

    }

    componentWillUnmount() { }

    componentDidShow() { }

    componentDidHide() { }

    //H5获取用户授权
    webEmpower() {
        phoneLogin.webEmpower({ url: this.props.url })
    }

    //获取用户授权
    getUserInfo(e) {
        console.log('授权进来了')
        if (Taro.getEnv() === Taro.ENV_TYPE.WEB) {
            this.webEmpower()
            return
        }
        Taro.login().then(res => {
            if (res.code) {
                // 查看是否授权
                Taro.getSetting().then(data => {
                    if (data.authSetting['scope.userInfo']) {
                        // Taro.authorize({ scope: 'scope.userInfo' }).then(() => {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                        allExtrance.util.Taro.showLoading('授权中')
                        Taro.getUserInfo().then(result => {
                            allExtrance.util.Taro.setStorageSync('userInfo', result)
                            phoneLogin.userInfo({ code: res.code, ...result }).then(resDa => {
                                if (resDa) {
                                    allExtrance.util.Taro.setStorageSync('hasWechatUser', true)
                                    this.props.onHasWechatUser()
                                    Taro.hideLoading()
                                }
                            }).catch(err => { allExtrance.util.Taro.showToast(err); this.props.onHasWechatUser() })
                        }).catch(error => { console.log(error) })
                        // })
                    }else{
                        allExtrance.util.Taro.showToast('您已拒绝授权')
                        this.props.user()
                    }
                })
            }
        }).catch(err => { console.log(err) })
    }

    //退出用户授权
    exitGetInfo(e) {
        this.props.user()
        e.stopPropagation()
    }
    stopPropagation(e) {
        e.stopPropagation()
    }

    render() {
        return (
            <View className='index'>
                <View className='getUserInfo'>
                    <View className='zhezhao' onClick={this.exitGetInfo.bind(this, 'resumeAlert')}>
                        <View className='messageBlock' onClick={this.stopPropagation.bind(this)}>
                            <View className='themeColor' style={`background-color:${Taro.getStorageSync('baseColor')};height:105px`}>
                                <View className='logoImg logoImgThird'>
                                    <Image src='https://file.open.banchengyun.com/353/64c5fb9f6756798e23cf182b525ad257'></Image>
                                </View>
                                <View className='cloud'>
                                    <Image src='https://file.open.banchengyun.com/2019/06/25/156145781925397.png' />
                                </View>
                            </View>
                            <View className='msgContent'>
                                <View className='msgTitle' style='font-weight:bold'>获取用户信息</View>
                                <View className='msgRow'>授权微信信息</View>
                                <View className='msgRow'>快速完善用户信息</View>
                                {
                                    Taro.getEnv() === Taro.ENV_TYPE.WEB
                                    ?<Button className='msgBtn' open-type='getUserInfo' onClick={this.getUserInfo.bind(this)} style={`background-color:${Taro.getStorageSync('baseColor')}`}>确定</Button>
                                    :<Button className='msgBtn' open-type='getUserInfo' onGetUserInfo={this.getUserInfo.bind(this)} style={`background-color:${Taro.getStorageSync('baseColor')}`}>确定</Button>
                                }
                                
                                {/* <View className='btnBlock'>
                                    <Button className='whiteBtn' onClick={this.keepApply.bind(this)}>继续投递</Button>
                                    <Button className='themeBtn' onClick={this.editResume.bind(this)} style={`background-color:${Taro.getStorageSync('baseColor')}`}>完善简历</Button>
                                </View> */}

                            </View>
                            <View className='msgClose' onClick={this.exitGetInfo.bind(this, 'resumeAlert')}>
                                <Image src='https://file.open.banchengyun.com/2019/06/25/156145781914338.png'></Image>
                            </View>
                        </View>
                    </View>
                </View>
            </View>
        )
    }
}

export default GetUserInfo
